<template>
<div :style='{"width":"100%","padding":"30px 7% 40px","margin":"10px auto","position":"relative","background":"none"}'>
    <el-form
      class="add-update-preview"
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="150px"
    >
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="场地编号" prop="changdibianhao">
            <el-input v-model="ruleForm.changdibianhao" 
                placeholder="场地编号" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="场地名称" prop="changdimingcheng">
            <el-input v-model="ruleForm.changdimingcheng" 
                placeholder="场地名称" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="场地类型" prop="changdileixing">
            <el-input v-model="ruleForm.changdileixing" 
                placeholder="场地类型" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="预约时长" prop="yuyueshizhang">
            <el-input v-model="ruleForm.yuyueshizhang" 
                placeholder="预约时长" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="可用时间" prop="keyongshijian">
            <el-input v-model="ruleForm.keyongshijian" 
                placeholder="可用时间" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="游客账号" prop="youkezhanghao">
            <el-input v-model="ruleForm.youkezhanghao" 
                placeholder="游客账号" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="游客姓名" prop="youkexingming">
            <el-input v-model="ruleForm.youkexingming" 
                placeholder="游客姓名" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","alignItems":"center","background":"none","display":"block"}' label="预约时间" prop="yuyueshijian" >
              <el-date-picker
                  value-format="yyyy-MM-dd HH:mm:ss"
                  v-model="ruleForm.yuyueshijian" 
                  type="datetime"
                  placeholder="预约时间">
              </el-date-picker>
          </el-form-item>

      <el-form-item :style='{"padding":"0","margin":"20px 0 0 0","alignItems":"center","justifyContent":"center","display":"flex"}'>
        <el-button :style='{"border":"2px solid #ba1f26","cursor":"pointer","padding":"0 35px","boxShadow":"inset 6px 0px 0px 0px #ba1f26","margin":"0 20px 0 0","color":"#ba1f26","outline":"none","borderRadius":"30px","background":"#fff","width":"auto","lineHeight":"44px","fontSize":"16px","height":"48px"}'  type="primary" @click="onSubmit">提交</el-button>
        <el-button :style='{"border":"2px solid #efa62a","cursor":"pointer","padding":"0 35px","boxShadow":"inset 6px 0px 0px 0px #efa62a","margin":"0","color":"#efa62a","outline":"none","borderRadius":"30px","background":"#fff","width":"auto","lineHeight":"44px","fontSize":"16px","height":"48px"}' @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
</div>
</template>

<script>
  export default {
    data() {
      return {
        id: '',
        baseUrl: '',
        ro:{
            changdibianhao : false,
            changdimingcheng : false,
            changdileixing : false,
            yuyueshizhang : false,
            keyongshijian : false,
            youkezhanghao : false,
            youkexingming : false,
            yuyueshijian : false,
        },
        type: '',
        userTableName: localStorage.getItem('UserTableName'),
        ruleForm: {
          changdibianhao: '',
          changdimingcheng: '',
          changdileixing: '',
          yuyueshizhang: '',
          keyongshijian: '',
          youkezhanghao: '',
          youkexingming: '',
          yuyueshijian: '',
        },
        rules: {
          changdibianhao: [
          ],
          changdimingcheng: [
          ],
          changdileixing: [
          ],
          yuyueshizhang: [
            { validator: this.$validate.isIntNumer, trigger: 'blur' },
          ],
          keyongshijian: [
          ],
          youkezhanghao: [
          ],
          youkexingming: [
          ],
          yuyueshijian: [
          ],
        },
      };
    },
    computed: {



    },
    created() {
	  //this.bg();
      let type = this.$route.query.type ? this.$route.query.type : '';
      this.init(type);
      this.baseUrl = this.$config.baseUrl;
      this.ruleForm.yuyueshijian = this.getCurDateTime()
    },
    methods: {
      getMakeZero(s) {
          return s < 10 ? '0' + s : s;
      },
      // 下载
      download(file){
        window.open(`${file}`)
      },
      // 初始化
      init(type) {
        this.type = type;
        if(type=='cross'){
          var obj = JSON.parse(localStorage.getItem('crossObj'));
          for (var o in obj){
            if(o=='changdibianhao'){
              this.ruleForm.changdibianhao = obj[o];
              this.ro.changdibianhao = true;
              continue;
            }
            if(o=='changdimingcheng'){
              this.ruleForm.changdimingcheng = obj[o];
              this.ro.changdimingcheng = true;
              continue;
            }
            if(o=='changdileixing'){
              this.ruleForm.changdileixing = obj[o];
              this.ro.changdileixing = true;
              continue;
            }
            if(o=='yuyueshizhang'){
              this.ruleForm.yuyueshizhang = obj[o];
              this.ro.yuyueshizhang = true;
              continue;
            }
            if(o=='keyongshijian'){
              this.ruleForm.keyongshijian = obj[o];
              this.ro.keyongshijian = true;
              continue;
            }
            if(o=='youkezhanghao'){
              this.ruleForm.youkezhanghao = obj[o];
              this.ro.youkezhanghao = true;
              continue;
            }
            if(o=='youkexingming'){
              this.ruleForm.youkexingming = obj[o];
              this.ro.youkexingming = true;
              continue;
            }
            if(o=='yuyueshijian'){
              this.ruleForm.yuyueshijian = obj[o];
              this.ro.yuyueshijian = true;
              continue;
            }
          }
        }
        // 获取用户信息
        this.$http.get(this.userTableName + '/session', {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            var json = res.data.data;
            if((json.youkezhanghao!=''&&json.youkezhanghao) || json.youkezhanghao==0){
                this.ruleForm.youkezhanghao = json.youkezhanghao
            }
            if((json.youkexingming!=''&&json.youkexingming) || json.youkexingming==0){
                this.ruleForm.youkexingming = json.youkexingming
            }
          }
        });
      },

    // 多级联动参数
      // 多级联动参数
      info(id) {
        this.$http.get('changdiyuyue/detail/${id}', {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            this.ruleForm = res.data.data;
          }
        });
      },
      // 提交
      onSubmit() {

        //更新跨表属性
        var crossuserid;
        var crossrefid;
        var crossoptnum;
        this.$refs["ruleForm"].validate(valid => {
          if(valid) {
            if(this.type=='cross'){
                 var statusColumnName = localStorage.getItem('statusColumnName');
                 var statusColumnValue = localStorage.getItem('statusColumnValue');
                 if(statusColumnName && statusColumnName!='') {
                     var obj = JSON.parse(localStorage.getItem('crossObj'));
                     if(!statusColumnName.startsWith("[")) {
                         for (var o in obj){
                             if(o==statusColumnName){
                                 obj[o] = statusColumnValue;
                             }
                         }
                         var table = localStorage.getItem('crossTable');
                         this.$http.post(table+'/update', obj).then(res => {});
                     } else {
                            crossuserid=Number(localStorage.getItem('userid'));
                            crossrefid=obj['id'];
                            crossoptnum=localStorage.getItem('statusColumnName');
                            crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
                     }
                 }
            }
            if(crossrefid && crossuserid) {
                 this.ruleForm.crossuserid=crossuserid;
                 this.ruleForm.crossrefid=crossrefid;
                 var params = {
                     page: 1,
                     limit: 10,
                     crossuserid:crossuserid,
                     crossrefid:crossrefid,
                 }
                 this.$http.get('changdiyuyue/list', {
                  params: params
                 }).then(res => {
                     if(res.data.data.total>=crossoptnum) {
                         this.$message({
                          message: localStorage.getItem('tips'),
                          type: 'success',
                          duration: 1500,
                         });
                          return false;
                     } else {
                         // 跨表计算


                          this.$http.post('changdiyuyue/add', this.ruleForm).then(res => {
                              if (res.data.code == 0) {
                                  this.$message({
                                      message: '操作成功',
                                      type: 'success',
                                      duration: 1500,
                                      onClose: () => {
                                          this.$router.go(-1);
                                      }
                                  });
                              } else {
                                  this.$message({
                                      message: res.data.msg,
                                      type: 'error',
                                      duration: 1500
                                  });
                              }
                          });
                     }
                 });
             } else {


                  this.$http.post('changdiyuyue/add', this.ruleForm).then(res => {
                     if (res.data.code == 0) {
                          this.$message({
                              message: '操作成功',
                              type: 'success',
                              duration: 1500,
                              onClose: () => {
                                  this.$router.go(-1);
                              }
                          });
                      } else {
                          this.$message({
                              message: res.data.msg,
                              type: 'error',
                              duration: 1500
                          });
                      }
                  });
             }
          }
        });
      },
      // 获取uuid
      getUUID () {
        return new Date().getTime();
      },
      // 返回
      back() {
        this.$router.go(-1);
      },
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.el-date-editor.el-input {
		width: auto;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__label {
	  padding: 0 10px 0 0;
	  color: #ba1f26;
	  background: none;
	  font-weight: 500;
	  width: 150px;
	  font-size: 16px;
	  line-height: 40px;
	  text-align: right;
	  min-width: 150px;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__content {
	  margin-left: 150px;
	}
	
	.add-update-preview .el-input /deep/ .el-input__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 0 10px;
	  outline: none;
	  color: #666;
	  width: 360px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview .el-select /deep/ .el-input__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 0 10px;
	  outline: none;
	  color: #666;
	  width: 320px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview .el-date-editor /deep/ .el-input__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 0 10px 0 30px;
	  outline: none;
	  color: #666;
	  width: 360px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview /deep/ .el-upload--picture-card {
		background: transparent;
		border: 0;
		border-radius: 0;
		width: auto;
		height: auto;
		line-height: initial;
		vertical-align: middle;
	}
	
	.add-update-preview /deep/ .upload .upload-img {
	  border: 1px dashed #ba1f26;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #ba1f26;
	  width: 120px;
	  font-size: 32px;
	  line-height: 120px;
	  text-align: center;
	  height: 120px;
	}
	
	.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
	  border: 1px dashed #ba1f26;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #ba1f26;
	  width: 120px;
	  font-size: 32px;
	  line-height: 120px;
	  text-align: center;
	  height: 120px;
	}
	
	.add-update-preview /deep/ .el-upload .el-icon-plus {
	  border: 1px dashed #ba1f26;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #ba1f26;
	  width: 120px;
	  font-size: 32px;
	  line-height: 120px;
	  text-align: center;
	  height: 120px;
	}
	
	.add-update-preview .el-textarea /deep/ .el-textarea__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 15px;
	  box-shadow: 0 0 0px rgba(64, 158, 255, .5);
	  outline: none;
	  color: #666;
	  width: 400px;
	  font-size: 14px;
	  height: 120px;
	}
</style>
